import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * 
 * @author robertas
 */
public class ReservationClient extends Thread {

	private static final String BASE_URL = "http://localhost:8888/TestTwo/TestServlet";
	private static final String PARAM_ACTION = "action";
	private static final String ACTION_CHECK = "check";
	private static final String ACTION_RESERVE = "reserve";
	private static final String ACTION_RESET = "reset";

	/**
	 * 
	 */
	public ReservationClient() {
		super();
	}

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Thread t1 = new ReservationClient();
		Thread t2 = new ReservationClient();

		t1.start();
		t2.start();
	}

	/**
	 * 
	 */
	public void run() {
		int tableId = 3;
		if (isTableAvailable(tableId)) {
			reserveTable(tableId);
		}
	}

	/**
	 * 
	 * @return
	 */
	public static boolean isTableAvailable(int tableId) {
		boolean result = false;
		String response = null;
		if (BASE_URL.startsWith("http://")) {
			// Send a GET request to the servlet
			try {
				// Send data
				String urlStr = BASE_URL;
				String action = PARAM_ACTION + "=" + ACTION_CHECK;
				urlStr += "?" + action;
				URL url = new URL(urlStr);
				URLConnection conn = url.openConnection();
				// Get the response
				BufferedReader rd = new BufferedReader(new InputStreamReader(
						conn.getInputStream()));
				StringBuffer sb = new StringBuffer();
				String line;
				while ((line = rd.readLine()) != null) {
					sb.append(line);
				}
				rd.close();
				response = sb.toString();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (response != null && response.length() > 0) {
			try {
				result = Integer.parseInt(response) > 0;
			} catch (Exception e) {
			}
		}
		return result;
	}

	/**
	 * 
	 * @return
	 */
	public static boolean reserveTable(int tableId) {
		boolean result = false;
		String response = null;
		if (BASE_URL.startsWith("http://")) {
			// Send a GET request to the servlet
			try {
				// Send data
				String urlStr = BASE_URL;
				String action = PARAM_ACTION + "=" + ACTION_RESERVE;
				urlStr += "?" + action;
				URL url = new URL(urlStr);
				URLConnection conn = url.openConnection();
				// Get the response
				BufferedReader rd = new BufferedReader(new InputStreamReader(
						conn.getInputStream()));
				StringBuffer sb = new StringBuffer();
				String line;
				while ((line = rd.readLine()) != null) {
					sb.append(line);
				}
				rd.close();
				response = sb.toString();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (response != null && response.length() > 0) {
			try {
				result = Integer.parseInt(response) >= 0;
			} catch (Exception e) {
			}
		}
		return result;
	}

	/**
	 * 
	 */
	public static void reset() {
		if (BASE_URL.startsWith("http://")) {
			// Send a GET request to the servlet
			try {
				// Send data
				String urlStr = BASE_URL;
				String action = PARAM_ACTION + "=" + ACTION_RESET;
				urlStr += "?" + action;
				URL url = new URL(urlStr);
				URLConnection conn = url.openConnection();
				// Get the response
				BufferedReader rd = new BufferedReader(new InputStreamReader(
						conn.getInputStream()));
				StringBuffer sb = new StringBuffer();
				String line;
				while ((line = rd.readLine()) != null) {
					sb.append(line);
				}
				rd.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
